package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Role;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@WebServlet("/role")
public class RoleServlet extends BaseServlet {

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
        int page = 1;
        int size = 3;
        if (StringUtils.isNotBlank(request.getParameter("pageNum"))) {
            page = Integer.parseInt(request.getParameter("pageNum"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            size = Integer.parseInt(request.getParameter("pageSize"));
        }
        PageInfo all = roleService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page", all);
        //System.out.println(all1);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/role/role-list.jsp").forward(request, response);
    }

    //跳转到新增页面
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("/WEB-INF/pages/role/role-add.jsp").forward(request, response);
    }

    //实现新增功能
    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Role role = BeanUtil.fillBean(request, Role.class);

        roleService.save(role);
        list(request, response);
    }

    //跳转编辑页面
    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        request.getRequestDispatcher("/WEB-INF/pages/role/role-update.jsp").forward(request, response);
    }

    //实现编辑功能
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Role role = BeanUtil.fillBean(request, Role.class);
        //调用业务层接口save
        roleService.update(role);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/role?operation=list");
    }

    //删除功能
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] ids = request.getParameterValues("ids");
        roleService.delete(ids);
        response.sendRedirect(request.getContextPath() + "/role?operation=list");
    }

    //修改权限数据回显
    public void author(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleId = request.getParameter("id");
        Role role = roleService.findById(roleId);
        request.setAttribute("role", role);
        List<Map> map = permissionService.findAuthorDataByRoleId(roleId);
        //map转成json数据
        ObjectMapper op = new ObjectMapper();
        String json = op.writeValueAsString(map);
        request.setAttribute("rolePermissionJson", json);
        //跳转到数页面
        request.getRequestDispatcher("/WEB-INF/pages/role/role-permission-add.jsp").forward(request, response);
    }

    //修改权限
    public void updateRolePermission(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String roleId = request.getParameter("roleId");
        String permissionIds = request.getParameter("permissionIds");
        System.out.println(permissionIds);
        roleService.updateRolePermission(roleId, permissionIds);
        //跳转回到页面list
        list(request, response);
        //response.sendRedirect(request.getContextPath() + "/role?operation=list");
    }

    public void findByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
       int page = 1;
        int size = 3;
         if (StringUtils.isNotBlank(request.getParameter("pageNum"))) {
            page = Integer.parseInt(request.getParameter("pageNum"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            size = Integer.parseInt(request.getParameter("pageSize"));
        }

        String name = request.getParameter("name");
        PageInfo namelist = roleService.findByName(page,size,name);
        request.setAttribute("page", namelist);
        request.getRequestDispatcher("/WEB-INF/pages/role/role-list.jsp").forward(request, response);
    }

}
